//
//  DCAuthInfoCommonCell.m
//  Demo
//
//  Created by zhaolin on 2023/1/12.
//

#import "DCAuthInfoCommonCell.h"
#import "DCAuthInfoUIModel.h"

@interface DCAuthInfoCommonCell ()<UITextFieldDelegate>
@property (nonatomic, assign) BOOL canEdit;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, strong) UIImageView *rightImage;

@property (nonatomic, strong) DCAuthInfoUIModel *model;
@end
@implementation DCAuthInfoCommonCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self confiugreUI];
    }
    return self;
}


- (void)configureDataWithModel:(DCAuthInfoUIModel *)model {
    _model = model;
    self.titleLabel.text = model.title;
    self.textField.text = model.content;
    self.canEdit = model.canEdit;
    NSAttributedString *placeH = [[NSAttributedString alloc] initWithString:model.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor colorWithString:@"#C7C7C7"],NSFontAttributeName:DCFont_PF_Regular(14)}];
    self.textField.attributedPlaceholder = placeH;
    if (model.celltype == DCAuthInfoCommonCellTypeWoring) {
        
    } else {
        if (model.celltype == DCAuthInfoCommonCellTypeRightRight) {
            self.rightImage.image = [UIImage imageNamed:@"rightInto"];
        } else if(model.celltype == DCAuthInfoCommonCellTypeRightDown) {
            self.rightImage.image = [UIImage imageNamed:@"rightDown"];
        }else {
            self.rightImage.image = [UIImage imageNamed:@""];
        }
        
    }
}

- (void)confiugreUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    
    _titleLabel = [UILabel new];
    _titleLabel.text = @"title";
    _titleLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    _titleLabel.font = DCFont_PF_Semibold(13);
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.top.mas_equalTo(15);
    }];
    
    UIView *whiteBG = [UIView new];
    whiteBG.layer.cornerRadius = 8;
    whiteBG.layer.masksToBounds = YES;
    whiteBG.layer.borderWidth = 1;
    whiteBG.layer.borderColor = [[UIColor colorWithString:@"#F7F7F7"] CGColor];
    [self.contentView addSubview:whiteBG];
    [whiteBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.height.mas_equalTo(54);
        make.right.mas_equalTo(-18);
        make.top.mas_equalTo(_titleLabel.mas_bottom).mas_offset(10);
    }];
    
    _textField = [UITextField new];
    _textField.delegate = self;
    _textField.font = DCFont_PF_Medium(14);
    _textField.textColor = [UIColor colorWithString:@"#1C221B"];
    [whiteBG addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(whiteBG);
        make.right.mas_equalTo(-35);
        make.height.mas_equalTo(40);
    }];
    
    _rightImage = [UIImageView new];
    [whiteBG addSubview:_rightImage];
    [_rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(whiteBG.mas_centerY);
    }];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if(self.delegate && [self.delegate respondsToSelector:@selector(authInfoCommonCellBegainEditingWithText:)]) {
        [self.delegate authInfoCommonCellBegainEditingWithText:self.indexPath];
    }
    return self.canEdit;
}        // return NO to disallow editing.

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(authInfoCommonCellEndEditingWithText:)]) {
        [self.delegate authInfoCommonCellEndEditingWithText:textField.text];
    }
    _model.content = textField.text;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
